package org.eerbaar.lang.expressions;

import java.util.ArrayList;
import java.util.List;

import org.eerbaar.lang.Expression;
import org.eerbaar.lang.HasRuntime;
import org.eerbaar.lang.L;
import org.eerbaar.lang.Values;
import org.eerbaar.lang.runtime.Executor;

public class Add implements Expression, HasRuntime {

    private List<Expression> expressions = new ArrayList<Expression>();
    private Expression resultVar = Values.UNDEF;

    public Add(Expression e1, Expression e2) {
        expressions.add(e1);
        expressions.add(e2);
    }

    public org.eerbaar.lang.runtime.Runtime getRuntime() {

        return new org.eerbaar.lang.runtime.Runtime() {

            @Override
            public void execute(Executor exec) {

                Integer result = new Integer(0);
                for (Expression e : expressions) {

                    exec.execute(e);
                    if (exec.getResult() instanceof Integer) {
                        Integer value = (Integer) exec.getResult();
                        result += value;
                    }
                }

                resultVar = L.integer(0);
                resultVar = new IntVar(result);
            }

            @Override
            public Object getResult() {
                return resultVar;
            }
        };
    }

    public String toString() {
        return "" + this.resultVar;
    }

}
